package com.example.robertluozizhao.framecollectdemo.activity;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.robertluozizhao.framecollectdemo.R;
import com.example.robertluozizhao.framecollectdemo.view.custom.VerticalInterceptListView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Lzz on 7/27/2019.
 * 内部拦截法
 * RecycleView 不能实现（内部有处理滑动冲突，当滑动到底部或顶部，事件交回父容器处理）
 * ListView 可以实现
 */

public class VerticalAndVerticalScrollActivity extends AppCompatActivity {

    @BindView(R.id.lv)
    VerticalInterceptListView mLv;
    @BindView(R.id.tv_top)
    TextView mTvTop;
    @BindView(R.id.tv_bottom)
    TextView mTvBottom;
    private String TAG = "VerticalAndVerticalScrollActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vertical_and_vertical_scroll);
        ButterKnife.bind(this, this);
        initData();
    }

    private void initData() {
        //初始化数据
        StringBuffer sbTop = new StringBuffer();
        StringBuffer sbBottom = new StringBuffer();
        CharSequence textTop = mTvTop.getText();
        CharSequence textBottom = mTvBottom.getText();
        for (int i = 0; i < 12; i++) {
            sbTop.append(textTop + "\n");
        }
        for (int i = 0; i < 20; i++) {
            sbBottom.append(textBottom + "\n");
        }
        mTvTop.setText(sbTop);
        mTvBottom.setText(sbBottom);

        final ArrayList<String> strings = new ArrayList<>();
        for (int i = 1; i < 21; i++) {
            strings.add("第" + i + "个");
        }

        //初始化ListView
        mLv.setParent((ViewGroup) findViewById(R.id.sv));
        mLv.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                if (strings != null) {
                    return strings.size();
                }
                return 0;
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder viewHolder = null;
                if (convertView == null) {
                    convertView = LayoutInflater.from(VerticalAndVerticalScrollActivity.this).inflate(R.layout.item_sticky_head, parent, false);
                    viewHolder = new ViewHolder();
                    viewHolder.setTextView((TextView) convertView.findViewById(R.id.tv_sticky_head));
                    convertView.setTag(viewHolder);
                } else {
                    viewHolder = (ViewHolder) convertView.getTag();
                }

                viewHolder.getTextView().setText(strings.get(position));

                return convertView;
            }
        });


        /*mLv.setOnScrollListener(new AbsListView.OnScrollListener() {
            private boolean isScroll;

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                switch (scrollState) {
                    case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                        isScroll = true;
                        break;
                    case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
                        isScroll = false;
                        break;
                }
            }

            *//**
             *
             * @param view 当前显示的ListView(局部)
             * @param firstVisibleItem 第一个item下标（相对于整个ListView）
             * @param visibleItemCount 当前显示的item count
             * @param totalItemCount 整个ListView 的 item count
             *//*
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

                *//**
                 * item View （bottom位置）底部到 listview 左上角的距离
                 *//*
                if (view == null) {
                    Log.d(TAG, "onScroll: view:" + view);
                    return;
                }

                int listViewHeight = view.getMeasuredHeight();
                int firstVisiblePosition = view.getFirstVisiblePosition();
                int lastVisiblePosition = view.getLastVisiblePosition();
                *//*View childAt1 = view.getChildAt(lastVisiblePosition);
                if (childAt1==null) {
                    return;
                }*//*
//                int lastViewBottm = childAt1.getMeasuredHeight();
                Log.d(TAG, "onScroll-->  firstVisibleItem:" + firstVisibleItem + ",visibleItemCount:" + visibleItemCount
                                + ",totalItemCount:" + totalItemCount + ",ListView total height:" + listViewHeight +
                                ",mLv.height:" + mLv.getMeasuredHeight() + ",firstVisiblePosition：" + firstVisiblePosition
                                + ",lastVisiblePosition:" + lastVisiblePosition
                *//*+",lastViewBottm:"+lastViewBottm*//*);

                //滑动到底部判断
                if (firstVisibleItem + visibleItemCount >= totalItemCount) {

                    *//*View childAt = mLv.getChildAt(lastVisiblePosition);

                    if (childAt==null) {
                        Log.d(TAG, "onScroll: childAt:"+childAt);
                        return;
                    }

                    if (lastViewBottm>=listViewHeight) {*//*
                    Log.d(TAG, "onScroll: setRequestDisallowIntercept(false)");

                 *//*   if (mLv.getDownOrUpMoveState()) {
                        mLv.setRequestDisallowIntercept(false);
                    }else {
                        mLv.setRequestDisallowIntercept(true);
                    }*//*

//                    }
                } else if (firstVisibleItem == 0) {
                    *//*if (mLv.getDownOrUpMoveState()) {
                        mLv.setRequestDisallowIntercept(true);
                    }else {
                        mLv.setRequestDisallowIntercept(false);
                    }*//*

                }
            }
        });*/
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * ListView ViewHolder
     */
    class ViewHolder {
        private TextView mTextView;

        public TextView getTextView() {
            return mTextView;
        }

        public void setTextView(TextView textView) {
            mTextView = textView;
        }
    }
}
